package mappers;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import domains.Message;

import managers.DatabaseConnection;

public class MessageMapper {
	
	//this method compose messages.
	public void composeMessage(Message m) {
		String sql = "insert into PPKEA.message (Subject, MessageText, Time) values (?,?,?)";
		try {
		DatabaseConnection dbConnect = new DatabaseConnection();
		PreparedStatement query = dbConnect.getConnect().prepareStatement(sql);
		
		query.setString(1, m.getSubject());
		query.setString(2, m.getMessageText());
		query.setString(3, m.getTime()+"");
		query.executeUpdate();
	
		} catch(Exception e) {
			e.printStackTrace();
		} 
	}
	
	//method that shows all messages
	public ArrayList<Message> showAllMessages() {
		ArrayList<Message> messages = new ArrayList<Message>();
		String sql = "SELECT Subject, MessageText, Time FROM PPKEA.message ORDER BY Time";
		try {
		DatabaseConnection dbConnect = new DatabaseConnection();
		PreparedStatement query = dbConnect.getConnect().prepareStatement(sql);
		
		ResultSet rs = query.executeQuery(sql);
	
		while(rs.next()) {
			Message m = new Message(rs.getString("Subject"), rs.getString("MessageText"), 
					rs.getString("Time"));
			
			messages.add(m);
		}
		
		} catch(Exception e) {
			e.printStackTrace();
		} 
		
		return messages;
		
	}
}
